<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BufferedReader 深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #569cd6; }
        .string { color: #ce9178; }
        .method { color: #dcdcaa; }
        .class-name { color: #4ec9b0; }
        .comment { color: #6a9955; }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 8px;
            margin-top: 4px;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    BufferedReader
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">Java 输入流的优雅之道</span>
                </h1>
                <p class="text-xl md:text-2xl font-light leading-relaxed opacity-90">
                    探索高效的缓冲区读取机制，掌握键盘输入的标准实践
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-6xl">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-book-open text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">核心概念</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed drop-cap">
                BufferedReader 类是 Java I/O 体系中的重要组成部分，它通过在内存中创建缓冲区来优化读取操作。所有输入字节数据都将先存储到缓冲区中，然后再进行处理，这种机制显著提升了 I/O 操作的效率，特别是在处理大量数据或频繁读取的场景中。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-diagram-project text-purple-600 mr-3"></i>
                数据流转架构
            </h3>
            <div class="mermaid">
                graph LR
                    A[键盘输入] -->|字节流| B[System.in]
                    B -->|包装| C[InputStreamReader]
                    C -->|字符流| D[BufferedReader]
                    D -->|缓冲区| E[应用程序]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#4ec9b0,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Code Example Section -->
        <div class="mb-12">
            <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-code text-purple-600 mr-3"></i>
                标准实现示例
            </h3>
            <div class="code-container">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="text-gray-400 ml-4 text-sm">Test01.java</span>
                </div>
                <pre><code><span class="keyword">public class</span> <span class="class-name">Test01</span> {
    <span class="keyword">public static void</span> <span class="method">main</span>(<span class="class-name">String</span>[] args) <span class="keyword">throws</span> <span class="class-name">Exception</span> {
        
        <span class="keyword">while</span>(<span class="keyword">true</span>) {
            <span class="comment">// 创建缓冲读取器，包装标准输入流</span>
            <span class="class-name">BufferedReader</span> buf = <span class="keyword">new</span> <span class="class-name">BufferedReader</span>(
                <span class="keyword">new</span> <span class="class-name">InputStreamReader</span>(<span class="class-name">System</span>.in)
            );
            
            <span class="comment">// 读取一行输入</span>
            <span class="class-name">String</span> read = buf.<span class="method">readLine</span>();
            <span class="class-name">System</span>.out.<span class="method">println</span>(read);
            
            <span class="comment">// 输入 "exit" 退出循环</span>
            <span class="keyword">if</span> (<span class="string">"exit"</span>.<span class="method">equals</span>(read)) {
                <span class="keyword">break</span>;
            }
        }
    }
}</code></pre>
            </div>
        </div>

        <!-- Features Grid -->
        <div class="grid md:grid-cols-3 gap-6 mb-12">
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-purple-600 text-4xl mb-4">
                    <i class="fas fa-tachometer-alt"></i>
                </div>
                <h4 class="text-xl font-bold text-gray-800 mb-3">高效缓冲</h4>
                <p class="text-gray-600">通过内存缓冲区减少系统调用次数，显著提升读取性能</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-purple-600 text-4xl mb-4">
                    <i class="fas fa-stream"></i>
                </div>
                <h4 class="text-xl font-bold text-gray-800 mb-3">字符流处理</h4>
                <p class="text-gray-600">自动处理字符编码转换，完美支持中文等多字节字符</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-purple-600 text-4xl mb-4">
                    <i class="fas fa-align-left"></i>
                </div>
                <h4 class="text-xl font-bold text-gray-800 mb-3">按行读取</h4>
                <p class="text-gray-600">readLine() 方法提供便捷的按行读取功能，简化文本处理</p>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-12">
            <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                最佳实践建议
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h5 class="font-semibold text-gray-800 mb-1">资源管理</h5>
                        <p class="text-gray-600">使用 try-with-resources 语句自动管理资源关闭</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h5 class="font-semibold text-gray-800 mb-1">异常处理</h5>
                        <p class="text-gray-600">妥善处理 IOException，确保程序健壮性</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h5 class="font-semibold text-gray-800 mb-1">编码指定</h5>
                        <p class="text-gray-600">明确指定字符编码，避免跨平台兼容性问题</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h5 class="font-semibold text-gray-800 mb-1">缓冲区大小</h5>
                        <p class="text-gray-600">根据实际需求调整缓冲区大小以优化性能</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Advanced Example -->
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                <i class="fas fa-rocket text-purple-600 mr-3"></i>
                进阶应用示例
            </h3>
            <div class="code-container">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="text-gray-400 ml-4 text-sm">AdvancedExample.java</span>
                </div>
                <pre><code><span class="keyword">try</span> (<span class="class-name">BufferedReader</span> reader = <span class="keyword">new</span> <span class="class-name">BufferedReader</span>(
        <span class="keyword">new</span> <span class="class-name">InputStreamReader</span>(<span class="class-name">System</span>.in, <span class="class-name">StandardCharsets</span>.UTF_8))) {
    
    <span class="class-name">System</span>.out.<span class="method">println</span>(<span class="string">"请输入命令（输入 'exit' 退出）："</span>);
    
    <span class="class-name">String</span> line;
    <span class="keyword">while</span> ((line = reader.<span class="method">readLine</span>()) != <span class="keyword">null</span> && !<span class="string">"exit"</span>.<span class="method">equalsIgnoreCase</span>(line)) {
        <span class="comment">// 处理输入的命令</span>
        <span class="method">processCommand</span>(line);
    }
} <span class="keyword">catch</span> (<span class="class-name">IOException</span> e) {
    <span class="class-name">System</span>.err.<span class="method">println</span>(<span class="string">"读取输入时发生错误: "</span> + e.<span class="method">getMessage</span>());
}</code></pre>
            </div>
        </div>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#7c3aed',
                lineColor: '#5b21b6',
                secondaryColor: